package com.jsgt.greekfire.ui.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fhx.core.goToActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.base.BaseBottomDialogFragment
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.wallet.ManageActivity
import com.jsgt.greekfire.ui.wallet.adapter.SelectWalletAdapter
import com.jsgt.greekfire.ui.wallet.adapter.WalletManageData
import org.jetbrains.anko.find

class SwitchWalletDialog : BaseBottomDialogFragment() {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }

    private val data: MutableList<WalletManageData> by lazy {
        ArrayList<WalletManageData>()
    }

    private val walletAdapter: SelectWalletAdapter by lazy {
        SelectWalletAdapter()
    }
    private var isAddTitle = false
    private var wallet: IdentityWallet? = null
    lateinit var selectListener: (identityWallet: IdentityWallet) -> Unit

    fun selectListener(selectListener: (identityWallet: IdentityWallet) -> Unit): SwitchWalletDialog {
        this.selectListener = selectListener
        return this
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_switch_wallet
    }

    override fun canCancel(): Boolean {
        return false
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.find<View>(R.id.imvClose).setOnClickListener {
            dismiss()
        }
        dialog.find<View>(R.id.txvManager).setOnClickListener {
            dismiss()
            goToActivity<ManageActivity>()
        }
        val recycler = dialog.find<RecyclerView>(R.id.recycler)
        recycler.layoutManager = LinearLayoutManager(requireContext())
        recycler.adapter = walletAdapter
        walletAdapter.setNewInstance(data)
        walletAdapter.setOnItemClickListener { _, _, position ->
            if (walletAdapter.getItemViewType(position) == WalletManageData.VIEW_TYPE_WALLET) {
                if (walletAdapter.getItem(position).data!!.selection == 0) {
                    walletViewModel.cleanSelection()
                    walletViewModel.updateSelection(1, walletAdapter.getItem(position).data!!.id)
                }
                dismiss()
            }
        }

        walletViewModel.getAllIdentityWallet()?.observe(this, Observer {
            data.clear()
            data.add(WalletManageData(WalletManageData.VIEW_TYPE_MANAGE_TITLE, null))
            isAddTitle = false
            wallet = it[0]
            it.forEach { it1 ->
                if (it1?.identityName.isNullOrEmpty()) {
                    if (!isAddTitle) {
                        data.add(WalletManageData(WalletManageData.VIEW_TYPE_ADD_TITLE, null))
                        isAddTitle = true
                    }
                }
                data.add(WalletManageData(WalletManageData.VIEW_TYPE_WALLET, it1))
            }
            walletAdapter.notifyDataSetChanged()
        })
        return dialog
    }
}